# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=ca-certificates
pkgver=20170717
pkgrel=2
pkgdesc='Common CA certificates'
arch=('any')
url='https://www.mozilla.org/en-US/about/governance/policies/security-group/certs/'
license=('MPL' 'GPL')
source=("http://ftp.debian.org/debian/pool/main/c/${pkgname}/${pkgname}_${pkgver}.tar.xz"
        'StartSSL.sca.server1.crt'::'https://www.startssl.com/certs/sca.server1.crt'
        'certdata2pem.py'
        'update-ca-trust'
        'update-ca-trust.8')
depends=('bash' 'openssl' 'findutils' 'coreutils' 'sed' 'p11-kit')
makedepends=('asciidoc' 'python2' 'libxslt' 'sed' 'grep')
install='ca-certificates.install'
sha256sums=('e487639b641fa75445174734dd6e9d600373e3248b3d86a7e3c6d0f6977decd2'
            'c5b24a798bd87e2fb710ccac374f56991d6939b80de3fe1a5a15211f1f9fca43'
            '4e96bd7424062d365b75247dfc4b3b6510f09ca5161c0f7c3ce76b10edf633aa'
            '0f3e97846494ad41330352bb6b2fa0f8d264bf16d8d02f04b759ca8c26b3e092'
            'a73c6430e734178b9aa4d303709470383bc2b1cfbeb0d44fe34615df812f479d')

prepare() {
  
  sed "s|/usr/bin/python|/usr/bin/python2|g" -i certdata2pem.py
  cp certdata2pem.py ${srcdir}/${pkgname}/mozilla/certdata2pem.py
  cd ${srcdir}/${pkgname}
  cp ${srcdir}/update-ca-trust sbin/
  cp ${srcdir}/update-ca-trust.8 sbin/
}

build() {
  cd ${srcdir}/${pkgname}/mozilla
  mkdir -p legacy-{default,disable}

  /usr/bin/python2 ./certdata2pem.py

  (
    cat <<EOF
# This is a bundle of X.509 certificates of public Certificate
# Authorities.  It was generated from the Mozilla root CA list.
# These certificates and trust/distrust attributes use the file format accepted
# by the p11-kit-trust module.
#
# Source: nss/lib/ckfw/builtins/certdata.txt
# Source: nss/lib/ckfw/builtins/nssckbi.h
#
# Generated from:
EOF
    cat nssckbi.h | grep -w NSS_BUILTINS_LIBRARY_VERSION | awk '{print "# " $2 " " $3}';
    echo '#';
  ) > ${srcdir}/${pkgname}/ca-bundle.trust.crt

  touch ${srcdir}/${pkgname}/ca-bundle.legacy.default.crt
  local NUM_LEGACY_DEFAULT=`find ./legacy-default -type f | wc -l`
  if [ $NUM_LEGACY_DEFAULT -ne 0 ]; then
     for f in ./legacy-default/*.crt; do 
       echo "processing $f"
       tbits=`sed -n '/^# openssl-trust/{s/^.*=//;p;}' $f`
       alias=`sed -n '/^# alias=/{s/^.*=//;p;q;}' $f | sed "s/'//g" | sed 's/"//g'`
       targs=""
       if [ -n "$tbits" ]; then
          for t in $tbits; do
             targs="${targs} -addtrust $t"
          done
       fi
       if [ -n "$targs" ]; then
          echo "legacy default flags $targs for $f" >> info.trust
          openssl x509 -text -in "$f" -trustout $targs -setalias "$alias" >> ${srcdir}/${pkgname}/ca-bundle.legacy.default.crt
       fi
     done
  fi

  touch ${srcdir}/${pkgname}/ca-bundle.legacy.disable.crt
  NUM_LEGACY_DISABLE=`find ./legacy-disable -type f | wc -l`
  if [ $NUM_LEGACY_DISABLE -ne 0 ]; then
     for f in ./legacy-disable/*.crt; do 
       echo "processing $f"
       tbits=`sed -n '/^# openssl-trust/{s/^.*=//;p;}' $f`
       alias=`sed -n '/^# alias=/{s/^.*=//;p;q;}' $f | sed "s/'//g" | sed 's/"//g'`
       targs=""
       if [ -n "$tbits" ]; then
          for t in $tbits; do
             targs="${targs} -addtrust $t"
          done
       fi
       if [ -n "$targs" ]; then
          echo "legacy disable flags $targs for $f" >> info.trust
          openssl x509 -text -in "$f" -trustout $targs -setalias "$alias" >> ${srcdir}/${pkgname}/ca-bundle.legacy.disable.crt
       fi
     done
  fi

  # Add custom certificate
  echo '# alias="StartCom Class 1 Primary Intermediate Server CA"' > ${srcdir}/${pkgname}/mozilla/StartSSL.sca.server1.crt
  echo '# trust=CKA_TRUST_CODE_SIGNING CKA_TRUST_EMAIL_PROTECTION CKA_TRUST_SERVER_AUTH' >> ${srcdir}/${pkgname}/mozilla/StartSSL.sca.server1.crt
  echo '# distrust=' >> ${srcdir}/${pkgname}/mozilla/StartSSL.sca.server1.crt
  echo '# openssl-trust=codeSigning emailProtection serverAuth' >> ${srcdir}/${pkgname}/mozilla/StartSSL.sca.server1.crt
  cat ${srcdir}/StartSSL.sca.server1.crt >> ${srcdir}/${pkgname}/mozilla/StartSSL.sca.server1.crt

  local P11FILES=`find . -name \*.tmp-p11-kit | wc -l`
  if [ $P11FILES -ne 0 ]; then
    for p in ./*.tmp-p11-kit; do 
      cat "$p" >> ${srcdir}/${pkgname}/ca-bundle.trust.crt
    done
  fi

}



package() {
  cd ${srcdir}/${pkgname}

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  mkdir -p ${pkgdir}/etc
  mkdir -p ${pkgdir}/usr/share/man/man8

  cp -f ${srcdir}/update-ca-trust ${pkgdir}/usr/bin/
  cp -f ${srcdir}/update-ca-trust.8 ${pkgdir}/usr/share/man/man8/

  # for p11-kit
  mkdir -p ${pkgdir}/usr/lib/p11-kit
  cp -f ${srcdir}/update-ca-trust ${pkgdir}/usr/lib/p11-kit/p11-kit-extract-trust

  mkdir -p ${pkgdir}/usr/share/pki/ca-trust-{source,legacy}
  install -p -m 644 ca-bundle.trust.crt          ${pkgdir}/usr/share/pki/ca-trust-source/ca-bundle.trust.crt
  install -p -m 644 ca-bundle.legacy.default.crt ${pkgdir}/usr/share/pki/ca-trust-legacy/ca-bundle.legacy.default.crt
  install -p -m 644 ca-bundle.legacy.disable.crt ${pkgdir}/usr/share/pki/ca-trust-legacy/ca-bundle.legacy.disable.crt

  # touch all files overwritten by update-ca-trust for easy cleanup
  mkdir -p ${pkgdir}/etc/pki/ca-trust/{extracted,source}
  mkdir -p ${pkgdir}/etc/pki/ca-trust/source/{anchors,blacklist}
  mkdir -p ${pkgdir}/etc/pki/ca-trust/extracted/{openssl,pem,java}
  touch ${pkgdir}/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
  touch ${pkgdir}/etc/pki/ca-trust/extracted/pem/{tls,email,objsign}-ca-bundle.pem
  touch ${pkgdir}/etc/pki/ca-trust/extracted/java/cacerts

  # for OpenSSL and static ca-certificates consumers
  mkdir -p ${pkgdir}/usr/ssl/certs
  cp -f ${pkgdir}/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem ${pkgdir}/usr/ssl/certs/ca-bundle.crt
  cp -f ${pkgdir}/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem ${pkgdir}/usr/ssl/cert.pem
  cp -f ${pkgdir}/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt ${pkgdir}/usr/ssl/certs/ca-bundle.trust.crt
}
